package dtd.PHS.VND_ExchangeRate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import dtd.PHS.VND_ExchangeRate.SimpleGestureFilter.SimpleGestureListener;
import dtd.PHS.VND_ExchangeRate.VCB_ERate.MyCurrency;
import dtd.PHS.VND_ExchangeRate.VCB_ERate.VND_ExchangeRate;


public class SendSMS extends Activity implements SimpleGestureListener{

	HashMap<CheckBox, MyCurrency> mapCB2Currency;
	ListView lvMain;
	TextView tvDate;
	VCB_ERate vcb;
	MyMainMenu mainMenu;
	private SimpleGestureFilter detector;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    MyUtility.callOnEveryActivity(this);
	    setContentView(R.layout.send_sms);
	    vcb = new VCB_ERate(this);
	    mainMenu = new MyMainMenu(this);
	    
	    mapCB2Currency = new HashMap<CheckBox, MyCurrency>();
	    tvDate = (TextView)findViewById(R.id.tvSendSMSDate);
	    lvMain = (ListView)findViewById(R.id.lv01);

	    this.updateDateText();
	    
	    lvMain.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
	    RowSMSAdapter adapter = new RowSMSAdapter(this,R.layout.row_sms_item,MyUtility.getCurrenciesExceptVND() );
	    lvMain.setAdapter(adapter);
	    this.detector = new SimpleGestureFilter(this, this);
	}
	
	private void updateDateText() {
		this.tvDate = (TextView)findViewById(R.id.tvSendSMSDate);
		tvDate.setText(tvDate.getText().toString() +" "+ vcb.getInputDate());		
	}
	
	public void onSendSMSClickHandler(View view) {
		Iterator<CheckBox> iterator = mapCB2Currency.keySet().iterator();
		ArrayList<VND_ExchangeRate> rates = new ArrayList<VND_ExchangeRate>();

		while( iterator. hasNext() ){
			CheckBox cb = iterator.next();
			MyCurrency currency = mapCB2Currency.get(cb);
			if ( cb.isChecked() ) {
				rates.add(vcb.getVNDRateOf(currency));
			}			
		}
		
		String content = MyUtility.createSMSContent(this, rates, vcb.getInputDate());
		MyUtility.broadcastSMSIntent(this, content);
	}
	
	public void onClickListRateHandler(View view) {
		Intent intent = new Intent(this,ListRates.class);
		this.startActivity(intent);
	}
	
	public void onClickConverterHandler(View view) {
		Intent intent = new Intent(this,CalculateRate.class);
		this.startActivity(intent);	
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
		mainMenu.createOptionsMenu(menu);
	    return true;
	}
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		return mainMenu.onItemSelected(item);	
	}
	
	private class RowSMSAdapter extends ArrayAdapter<MyCurrency> {
		HashMap<MyCurrency,SMSRowItem> viewOfCurrency;
		public RowSMSAdapter(Context context, int textViewResourceId,
				MyCurrency[] currencies) {
			super(context, textViewResourceId,currencies);
			viewOfCurrency = new HashMap<MyCurrency, SMSRowItem>();
		}

		@Override
		public View getView(
				int position,
				View convertView,
				ViewGroup parent ) {
			MyCurrency c = getItem(position);
			if ( c != null) {
				SMSRowItem v = this.viewOfCurrency.get(c);
				if ( v == null ) {				
					v = new SMSRowItem(getContext());
					this.viewOfCurrency.put(c, v);
				}						
				v.setFlag(c);
				v.mappingCheckboxCurrency(c,mapCB2Currency);
				return v;
			} else return null;
		}
	}
	@Override 
	public boolean dispatchTouchEvent(MotionEvent me){ 
		this.detector.onTouchEvent(me);
		return super.dispatchTouchEvent(me); 
	}
	
	@Override
	public void onDoubleTap() {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onSwipe(int direction) {
		switch (direction) {
		case SimpleGestureFilter.SWIPE_LEFT:
			this.startActivity(new Intent(this,ListRates.class));
			break;
		case SimpleGestureFilter.SWIPE_RIGHT:
			this.startActivity(new Intent(this,CalculateRate.class));
			break;
		default:
			break;
		}
		
	}
}
